var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
Page({
    data: {
        shareList: [],
        img_url: c.img_url,
        page: 1,
        share_point: 0,
        share_start_content: "",
        share_end_content: "",
        navTab:0,
        navlist:[
            '商品',
            '店铺'
        ]
    },
    onLoad: function (options) {
        utils.checkLogin();
        this.getData();
        this.getShareInfo();
    },
    //下拉刷新
    onPullDownRefresh: function () {
        this.setData({
            shareList: [],
            page: 1,
        });
        this.getData();
    },
    //上拉加载
    onReachBottom: function () {
        this.getData();
    },
    onShow: function () {

    },
    getData: function () {
        var that = this;
        var token = wx.getStorageSync("userinfo").wap_token;
        var data = {
            url: "/wsgoods/usershare/lists",
            token: token,
            page: that.data.page,
            limit: 10,
            type: that.data.navTab
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                var data = res.data;
                if (data.length > 0) {
                    that.setData({
                        page: that.data.page + 1
                    });
                }
                that.setData({
                    shareList: that.data.shareList.concat(data),
                });
            } else {

            }
            wx.stopPullDownRefresh();
        });
    },
    delshare: function (e) {
        var goods_id = 1;
        var that = this;
        var token = wx.getStorageSync("userinfo").wap_token;
        var data = {
            url: "/wsgoods/usershare/del",
            token: token,
            goods_id: goods_id
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                var data = res.data;
            } else {

            }
        });
    },
    //分享
    onShareAppMessage: function (res) {
      var that = this;
      if (res.from === "button") {
        var type = res.target.dataset.type;
        if (type == 1){
          var id = res.target.dataset.id;
          var name = res.target.dataset.name;
          var pic = res.target.dataset.pic;
          return {
            title: name,
            path: 'pages/template/shop/index?id=' + id,
            imageUrl: pic,
            success: function (res) {
              wx.getSystemInfo({
                success: function (d) {
                  var token = wx.getStorageSync("userinfo").wap_token;
                  var data = {
                    url: "/wsgoods/usershare/add",
                    token: token,
                    type: 1,
                    id: id
                  };
                  utils.request(data).then(function (res) {

                  });
                }
              })
            },
          }
        }else{
          var id = res.target.dataset.id;
          var name = res.target.dataset.name;
          var pic = res.target.dataset.pic;
          var description = res.target.dataset.description;
          var price = res.target.dataset.price;
          return {
            title: that.data.share_start_content + name + " ￥" + price + that.data.share_end_content,
            path: '/pages/shop/goodsdetail/index?id=' + id + "&invitecode=" + wx.getStorageSync("userinfo").invitecode,
            imageUrl: pic,
            success: function (res) {
              wx.getSystemInfo({
                success: function (d) {
                  var token = wx.getStorageSync("userinfo").wap_token;
                  var data = {
                    url: "/wsgoods/usershare/add",
                    token: token,
                    type: 0,
                    id: id
                  };
                  utils.request(data).then(function (res) {
                    if (res.code == 0) {

                    }
                  });
                }
              })
            },
          }
        }

      }
    },
    jump: function (res) {
      utils.r("/pages/shop/goodsdetail/index?id=" + res.currentTarget.dataset.id);
    },
    jumpshop: function (res) {
      console.log(res.currentTarget.dataset.id);
      utils.r("/pages/template/shop/index?id=" + res.currentTarget.dataset.id);
    },
    //分享信息
    getShareInfo: function () {
      var that = this;
      var data = {
        url: "/zfcom/com/deploy",
        uuid: "share_point,share_start_content,share_end_content",
      };
      utils.request(data).then(function (res) {
        if (res.code == 0) {
          that.setData({
            share_point: res.data.share_point,
            share_start_content: res.data.share_start_content,
            share_end_content: res.data.share_end_content,
          });
        }
      });
    },
    tabNav:function(e){
        let k = e.currentTarget.dataset.index;
        this.setData({
            navTab:k,
            shareList: [],
            page: 1,
        });
        this.getData();
    }

})